﻿namespace TestApp
{
    using System;
    using System.IO.IsolatedStorage;

    internal abstract class BaseStorage : IStorage
    {
        protected abstract string Extension { get; }

        protected IsolatedStorageFile storage
        {
            get { return IsolatedStorageFile.GetUserStoreForApplication(); }
        }

        string ConvertTypeToKey(Type type)
        {
            return string.Format("{0}.{1}", type.FullName.ToLower(), Extension);
        }

        public T ReadFromType<T>()
        {
            return ReadFromKey<T>(ConvertTypeToKey(typeof(T)));
        }

        public abstract T ReadFromKey<T>(string key);

        public void WriteWithType<T>(T obj)
        {
            WriteWithKey(obj, ConvertTypeToKey(typeof(T)));
        }

        public abstract void WriteWithKey(object obj, string key);
    }
}
